var UIController = require("UIController");
var CoinController = require("CoinController");
var PlayerController = require("PlayerController");
var PlayerHealthController = require("PlayerHealthController");

var PlayerStatValue = cc.Class({
	name: 'PlayerStatValue',
	properties: {
		cost: cc.Integer,
		value: cc.Float,
	},
	ctor (...params) {
		this.cost = params[0];
		this.value = params[1];
	},
});

var PlayerStatController = cc.Class({
    extends: cc.Component,

    properties: {
        moveSpeed: [PlayerStatValue],
		health: [PlayerStatValue],
		pickUpRange: [PlayerStatValue],
		maxWeapons: [PlayerStatValue],

		moveSpeedLevelCount: 0,
		healthLevelCount: 0,
		pickUpRangeLevelCount: 0,

		moveSpeedLevel: 0,
		healthLevel: 0,
		pickUpRangeLevel: 0,
		maxWeaponsLevel: 0,
    },

	statics: {
		instance: null,
	},
	ctor() {
		PlayerStatController.instance = this;
	},

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
		for (let i = this.moveSpeed.length - 1; i < this.moveSpeedLevelCount; i++) {
			this.moveSpeed.push(new PlayerStatValue(this.moveSpeed[i].cost + this.moveSpeed[1].cost, 
				this.moveSpeed[i].value + this.moveSpeed[1].value - this.moveSpeed[0].value));
		}

		for (let i = this.health.length - 1; i < this.healthLevelCount; i++) {
			this.health.push(new PlayerStatValue(this.health[i].cost + this.health[1].cost,
				this.health[i].value + this.health[1].value - this.health[0].value));
		}

		for (let i = this.pickUpRange.length - 1; i < this.pickUpRangeLevelCount; i++) {
			this.pickUpRange.push(new PlayerStatValue(this.pickUpRange[i].cost + this.pickUpRange[1].cost,
				this.pickUpRange[i].value + this.pickUpRange[1].value - this.pickUpRange[0].value));
		}
    },

    update (dt) {
		if (UIController.instance.levelUpPanel.active == true) {
			this.updateDisplay();
		}
	},

	updateDisplay () {
		if (this.moveSpeedLevel < this.moveSpeed.length - 1) {
			UIController.instance.moveSpeedUpgradeDisplay.updateDisplay(this.moveSpeed[this.moveSpeedLevel + 1].cost,
				this.moveSpeed[this.moveSpeedLevel].value, this.moveSpeed[this.moveSpeedLevel + 1].value);
		} else {
			UIController.instance.moveSpeedUpgradeDisplay.showMaxLevel();
		}

		if (this.healthLevel < this.health.length - 1) {
			UIController.instance.healthUpgradeDisplay.updateDisplay(this.health[this.healthLevel + 1].cost,
				this.health[this.healthLevel].value, this.health[this.healthLevel + 1].value);
		} else {
			UIController.instance.healthUpgradeDisplay.showMaxLevel();
		}

		if (this.pickUpRangeLevel < this.pickUpRange.length - 1) {
			UIController.instance.pickUpRangeUpgradeDisplay.updateDisplay(this.pickUpRange[this.pickUpRangeLevel + 1].cost,
				this.pickUpRange[this.pickUpRangeLevel].value, this.pickUpRange[this.pickUpRangeLevel + 1].value);
		} else {
			UIController.instance.pickUpRangeUpgradeDisplay.showMaxLevel();
		}

		if (this.maxWeaponsLevel < this.maxWeapons.length - 1) {
			UIController.instance.maxWeaponsUpgradeDisplay.updateDisplay(this.maxWeapons[this.maxWeaponsLevel + 1].cost,
				this.maxWeapons[this.maxWeaponsLevel].value, this.maxWeapons[this.maxWeaponsLevel + 1].value);
		} else {
			UIController.instance.maxWeaponsUpgradeDisplay.showMaxLevel();
		}
	},

	purchaseMoveSpeed () {
		this.moveSpeedLevel++;
		CoinController.instance.spendCoins(this.moveSpeed[this.moveSpeedLevel].cost);
		this.updateDisplay();
		PlayerController.instance.maxSpeed = this.moveSpeed[this.moveSpeedLevel].value;
	},

	purchaseHealth (){
		this.healthLevel++;
		CoinController.instance.spendCoins(this.health[this.healthLevel].cost);
		this.updateDisplay();
		PlayerHealthController.instance.maxHealth = this.health[this.healthLevel].value;
		PlayerHealthController.instance.currentHealth +=
			this.health[this.healthLevel].value - this.health[this.healthLevel - 1].value;
	},

	purchasePickUpRange () {
		this.pickUpRangeLevel++;
		CoinController.instance.spendCoins(this.pickUpRange[this.pickUpRangeLevel].cost);
		this.updateDisplay();
		PlayerController.instance.pickUpRange = this.pickUpRange[this.pickUpRangeLevel].value;
	},

	purchaseMaxWeapons () {
		this.maxWeaponsLevel++;
		CoinController.instance.spendCoins(this.maxWeapons[this.maxWeaponsLevel].cost);
		this.updateDisplay();
		PlayerController.instance.maxWeaponNumber = Math.round(this.maxWeapons[this.maxWeaponsLevel].value);
	}
});

module.exports = PlayerStatController;

